<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\DetailView;
use yii\bootstrap\ActiveForm;
use yii\widgets\Pjax;
//use kartik\select2\Select2;

// 获取订单所有联系人
$all = $product->allOrder;
$people = yii\helpers\ArrayHelper::map($all, 'username', 'username');

// 获取订单所有附加费
$type = $product->priceType;
$price_type = yii\helpers\ArrayHelper::map($type, 'id', 'name');

// 订单状态
$status = Yii::$app->params['order_status'];
unset($status[10]);
unset($status[11]);
unset($status[12]);
?>
<?PHP Pjax::begin(['id'=>'assign-user', 'timeout'=>3000, 'linkSelector'=>'#assign-user a[data-sort]', 'enablePushState'=>false]);?>
<style type="text/css">
	.order-view font {color:#9E9A9A;margin-right:10px;}
	.t_header {letter-spacing:2px}
	#_order {font-weight:bold; color: red;}
	#_people {font-weight:bold; color: red;}
	.modal-dialog { width: 1000px;}

	.list-group p {float:left;}
	.list-group label {height:25px;}
	/* .style-relation {background: rgba(166, 100, 166,0.1);} */
</style>

<?= yii\web\View::registerCssFile("/css/magic-check.css",['depends' => 'app\assets\AppAsset']) ?>
<div class="order-view">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	
		<!-- 显示汇总信息 -->
		<div class="t_header">
			<b><?= $assign->name ?> </b> :
			分配 <span id="_order"><?= count($data)?></span> 单   
			共计 <span id="_people"><?= $total?></span>  人
		</div>
	</div>
	<div class="row" style="padding:5px 15px;">

		<div class="col-md-4">

			<!-- main  add order customer content -->
			<ul id="add_order_customer" class="list-group">
				<?php foreach($data as $o){ ?>
					<li class='list-group-item'  id="check_<?= $o->id ?>" >
                		<input class="magic-checkbox assign-magic" <?= in_array($o->id, array_keys($data)) ? 'checked' : '' ?>  type="checkbox" id="ass_<?= $o->id?>" name="<?= $o->id?>" data-num="<?= $o->customerCount ?>" >
						<label for="ass_<?= $o->id?>" >
						<span><?= $o->username ?> <?= $o->code?> (<?= $o->customerCount?>)</span>
						</label>
					</li>
				<?php } ?>
			</ul>
		</div>
		

		<div class="col-md-8">
			<p>
    			<?php $form = ActiveForm::begin([
								'method' => 'get',
								'action' => Url::to(['/order/order/assign-user', 'product_id'=>$product->id, 'assign_id'=>$assign->id]),
        						'options' => ['data-pjax'=>true, 'class'=>'form-inline'],
                            ]); ?>
				<div class="form-group">
					联系人: 
					<select name="userName" class="form-control">
							<option value="">全部</option>
						<?php foreach($people as $key=>$val){ ?>
							<option value="<?= $key?>"><?= $val?></option>
						<?php } ?>
					</select>&nbsp;&nbsp;
				</div>


				<div class="form-group">
					状态 : 
					<select name="status" class="form-control">
						<option value="">全部</option>
						<?php foreach($status as $key=>$val){ ?>
							<option value="<?= $key?>"><?= $val?></option>
						<?php } ?>
					</select>&nbsp;&nbsp;
				</div>
				<div class="form-group">
					<!-- select name="priceType" class="form-control">
						</?php foreach($price_type as $k=>$v){ ?>
							<option value="</?= $k?>"></?= $v?></option>
						</?php } ?>
					</select -->
					<?php foreach($price_type as $k=>$v){ ?>
						<label><input type="checkbox" name="priceType[]" value=<?= $k ?> ><?= $v?></label>&nbsp;
					<?php } ?>
				</div>
				<button type="submit" class="btn btn-info btn-sm">查询</button>
    		<?php ActiveForm::end(); ?>
			</p>
			<ul class="list-group">
				<?php foreach($data2 as $o){ ?>
					<li class="list-group-item <?= ($o->isRelation > 1) ? 'style-relation' : '' ?>">
        	        	<input class="magic-checkbox assign-magic" <?= in_array($o->id, array_keys($data)) ? 'checked' : '' ?>  type="checkbox" id="ass_<?= $o->id?>" name="<?= $o->id?>" data-num="<?= $o->customerCount ?>" data-name="<?= $o->username?>" data-phone="<?= $o->phone ?>" data-code="<?= $o->code ?>">
						<label for="ass_<?= $o->id?>" >
							<p style="width:15%;"><?= $o->username ?></p>
							<!-- ?= $o->phone ? -->
							<p style="width:40%;"><?= $o->company ?>&nbsp;&nbsp;(<?= $o->customerCount ?>/<?= $o->roomNums?> 人)</p>
							<?php 
								if($o->priceAll ){
									foreach($o->priceAll as $price){ 
										if($price->type){
											echo "<font style='margin-left:8px;'><small>";
											print_r($price->typeObj->name .' ('.$price->num.'人) / ') ;
											echo "</small></font>";
										}
									}
								}
							?>
                            <p style="width:20%;"><?= $o->member->username ?></p>
                            <p style="width:20%;"><?= $o->code ?></p>
							<a class="pull-right"><?= ($o->isRelation > 1) ? '<i class="fa fa-link" title="关联单标识"></i>' : '' ?></a>
						</label>
					</li>
				<?php } ?>
			</ul>
		</div>
	</div>

</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
var assign_id = "<?= $assign->id ?>";
$(".assign-magic").click(function(){
    var order_id   = $(this).attr('name'); // 获取订单编号
    var num = $(this).data('num'); // 获取订单客户人数 
    var name = $(this).data('name'); // 获取订单客户姓名 
    var phone = $(this).data('phone'); // 获取订单客户联系方式
    var code = $(this).data('code'); // 订单编号
    var checked = $(this).is(':checked');   // 是否选中

	var order  = $("#_order").text();  //  订单数量 
	var people = $("#_people").text();  // 人员数量 

	if(checked){
		var html = "<li class='list-group-item' id='check_"+order_id+"'>"+name+" "+code+"<span class='badge'>"+ num +"</span></li>";
		
    	$.get("/order/order/assign-user-save", {order_id:order_id, assign_id:assign_id, checked:checked}, function(data){
			if(data){
				$("#add_order_customer").append(html);
				$("#_order").text(parseInt(order)+1);
				$("#_people").text(parseInt(people)+num);
			}else{
				alert('分配失败');
			}
    	});
	}else{

    	$.get("/order/order/assign-user-save", {order_id:order_id, assign_id:assign_id, checked:checked}, function(data){
			if(data){

				var id = "check_"+order_id;
				$("#"+id).remove();
	
				$("#_order").text(parseInt(order)-1);
				$("#_people").text(parseInt(people)-num);
			}else{
				alert('取消失败');
			}
    	});
	}

});
</script>
<?php app\components\JsBlock::end() ?>
<?php Pjax::end(); ?>
